package com.lujianfei.recyclerview.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lujianfei.recyclerview.R;
import com.lujianfei.recyclerview.entity.ListItem;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerMultiTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_ITEM_1 = 0;
    public static final int TYPE_ITEM_2 = 1;

    List<ListItem> listItemList;

    public RecyclerMultiTypeAdapter(List<ListItem> listItemList) {
        this.listItemList = listItemList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ITEM_1:{
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_header, parent, false);
                return new HeaderViewHolder(view);
            }
            case TYPE_ITEM_2: {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_item, parent, false);
                return new ItemViewHolder(view);
            }
            default:{
                return null;
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)){
            case TYPE_ITEM_1:{
                HeaderViewHolder itemViewHolder = (HeaderViewHolder) holder;
                ListItem listItem = this.listItemList.get(position);
                itemViewHolder.iv_header.setBackgroundResource(listItem.getColor());
                itemViewHolder.title.setText(listItem.getTitle());
            }
            break;
            case TYPE_ITEM_2:{
                ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
                ListItem listItem = this.listItemList.get(position);
                itemViewHolder.title.setText(listItem.getTitle());
                itemViewHolder.summary.setText(listItem.getSummary());
            }
            break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return listItemList.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return this.listItemList.size();
    }

    static class HeaderViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        ImageView iv_header;
        TextView title;

        public HeaderViewHolder(View view) {
            super(view);
            contentView = view;
            iv_header = view.findViewById(R.id.iv_header);
            title = view.findViewById(R.id.title);
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView title;
        TextView summary;

        public ItemViewHolder(View view) {
            super(view);
            contentView = view;
            title = view.findViewById(R.id.title);
            summary = view.findViewById(R.id.summary);
        }
    }
}
